package org.jsola.hr.web;

import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SalaryBillAddDTO;
import org.jsola.hr.dto.SalaryBillUpdateDTO;
import org.jsola.hr.dto.excel.SalaryBillImportDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.SalaryBillQuery;
import org.jsola.hr.service.ISalaryBillService;
import org.jsola.hr.vo.SalaryBillImportExcelVO;
import org.jsola.hr.vo.SalaryBillListRelationVO;
import org.jsola.hr.vo.SalaryBillListVO;
import org.jsola.hr.vo.SalaryBillVO;
import org.jsola.hr.vo.excel.ParseResultVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.List;


/**
 * 工资条
 *
 * @author zhr
 */
@RestController("hrSalaryBillController")
@RequestMapping(HrConstants.API_PATH + "/v1/salaryBills")
public class SalaryBillController implements ISalaryBillControllerAPI {

    @Autowired
    private ISalaryBillService salaryBillService;

    @Override
    @PostMapping
    public Result<SalaryBillVO> save(@Valid @RequestBody SalaryBillAddDTO salaryBillAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增工资条权限校验

        return Result.success(
                salaryBillService.save(salaryBillAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SalaryBillUpdateDTO salaryBillUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资条权限校验

        int count = salaryBillService.update(salaryBillUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{salaryBillId}")
    public Result<Integer> delete(@PathVariable String salaryBillId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除工资条权限校验

        int count = salaryBillService.deleteByIds(tokenUser, salaryBillId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] salaryBillIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除工资条权限校验

        int count = salaryBillService.deleteByIds(tokenUser, salaryBillIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{salaryBillId}")
    public Result<SalaryBillVO> findById(@PathVariable String salaryBillId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条权限校验

        return Result.success(
            salaryBillService.selectById(salaryBillId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SalaryBillListVO>> listAll(@Valid SalaryBillQuery salaryBillQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条权限校验

        return Result.success(
            salaryBillService.select(salaryBillQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SalaryBillListVO>> page(@Valid SalaryBillQuery salaryBillQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            salaryBillService.selectPage(salaryBillQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("/parse")
    public Result<List<ParseResultVO<SalaryBillImportDTO>>> parseImportFile(@RequestParam("file") MultipartFile file, @RequestParam(required = false) Boolean success) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 导入解析excel权限校验

        // 临时保存到本地
        String excelFilePath = FileKit.saveFileToLocal(file,null,null,new String[]{"xls","xlsx"},null);
        File excelFile = new File(excelFilePath);
        HrException.isTrue(excelFile.exists(), "文件上传失败");
        return Result.success(
                salaryBillService.parseImportFile(excelFile, success, tokenUser)
        );
    }

    @Override
    @PostMapping("/import")
    public Result<SalaryBillImportExcelVO> importFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(required = false) Boolean force,
            @RequestParam("companyId") String companyId,
            @RequestParam("year") String year,
            @RequestParam("month") String month,
            @RequestParam("tableName") String tableName){
        TokenUser tokenUser = UserContext.getUser();
        // todo 导入excel权限校验

        // 临时保存到本地
        String excelFilePath = FileKit.saveFileToLocal(file,null,null,new String[]{"xls","xlsx"},null);
        File excelFile = new File(excelFilePath);
        HrException.isTrue(excelFile.exists(), "文件上传失败");
        return Result.success(
                salaryBillService.importFile(excelFile, tokenUser, force, companyId, year, month, tableName)
        );
    }

    @Override
    @GetMapping(value = "pageRelation")
    public Result<SalaryBillListRelationVO> pageRelation(@Valid SalaryBillQuery salaryBillQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                salaryBillService.pageRelation(salaryBillQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "listAllRelation")
    public Result<SalaryBillListRelationVO> listAllRelation(@Valid SalaryBillQuery salaryBillQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                salaryBillService.listAllRelation(salaryBillQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getSalaryBillRelation")
    public Result<SalaryBillVO> getSalaryBillRelation(@Valid SalaryBillUpdateDTO salaryBillUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条发放信息权限校验

        return Result.success(
                salaryBillService.getSalaryBillRelation(salaryBillUpdateDTO, tokenUser.getSiteId())
        );
    }

}

